#ifndef CURRENTTHREAD_H
#define CURRENTTHREAD_H
#pragma once

/**
 * 当前进程
 */
namespace CurrentThread {

    //线程缓存编号 https://blog.csdn.net/qq_30071413/article/details/95205072
    extern __thread int threadCacheId;

    extern __thread char threadIdString[32];

    extern __thread unsigned int threadIdLength;

    extern __thread const char *threadName;

    void threadCacheThreadId();

    inline int getThreadCacheId()
    {
        // https://www.jianshu.com/p/2684613a300f
        /**
         * 这个指令是gcc引入的，作用是允许程序员将最有可能执行的分支告诉编译器。这个指令的写法为：__builtin_expect(EXP, N)。
         * 意思是：EXP==N的概率很大。
         */
        if (__builtin_expect(threadCacheId == 0, 0)) {
            threadCacheThreadId();
        }

        return threadCacheId;
    }

    inline char *getThreadIdString()
    {
        return threadIdString;
    }

    inline unsigned int getThreadIdLength()
    {
        return threadIdLength;
    }

    inline const char *getThreadName()
    {
        return threadName;
    }
}
#endif // CURRENTTHREAD_H
